<?php
namespace TuxCocktail\TuxDb\Mysql\Cocktail\Model;

use Zend\Db\TableGateway\AbstractTableGateway,
    Zend\Db\Adapter\Adapter,
    Zend\Db\ResultSet\ResultSet,
    Zend\Db\Sql\Select;

class PendingDrinkCommitsTable extends AbstractTableGateway
{
    protected $table ='pending_drink_commits';
    protected $tableName ='pending_drink_commits';

    public function qi($name)  { return $this->adapter->platform->quoteIdentifier($name); }
    
    public function fp($name) { return $this->adapter->driver->formatParameterName($name); }

    public function __construct(Adapter $adapter)
    {
        $this->adapter = $adapter;
        $this->resultSetPrototype = new ResultSet(new PendingDrinkCommits);

        $this->initialize();
    }

    public function fetchAll()
    {
        $resultSet = $this->select();
        return $resultSet;
    }
    
   	public function newSelect() {
    	return new Select;
    }
    
    public function getSelect(&$select,$columnsArray=array()) 
    {
    	$select = new Select;
    	return $select->from('pending_drink_commits')->columns($columnsArray);    	
    }
    
    public function createIfNotExist($checkColumnsArray,$optionalColumns=array(),&$isRowCreated=null) {
			$rowset=$this->select($checkColumnsArray);
    		$row = $rowset->current();
    		$id=null;
    		if ($row == null) {
    			$allColumns=array_merge($checkColumnsArray,$optionalColumns);
    			$affectedRows = $this->insert($allColumns);
    			if ($affectedRows != 1) {
    				throw new \Exception("error: could not add line to db");
    			}
    			$id=$this->lastInsertValue;
    			$isRowCreated=true;
    		} else {
    			$id=$row->pending_drink_commits_id;
    			$isRowCreated=false;
    		}
    		return $id;
    }
    
    //http://stackoverflow.com/questions/6156942/how-do-i-insert-an-empty-row-but-have-the-autonumber-update-correctly
    
    public function createEmptyRow() {
    	$row=array(
    	'pending_drink_commits_id' => null
    	);
    	$affectedRows=$this->insert($row);
 		if ($affectedRows != 1) {
    		throw new \Exception("error: could not add empty row to db");
    	}
    	$id=$this->lastInsertValue;
    	return $id;
	}
    
    public function getPendingDrinkCommits($id)
    {
        $id  = (int) $id;
        $rowset = $this->select(array('pending_drink_commits_id' => $id));
        $row = $rowset->current();
        if (!$row) {
            throw new \Exception("Could not find row $id");
        }
        return $row;
    }
    
     public function matchPendingDrinkCommits($pending_drink_commits_userid, $drink_brand, $drink_type, $company_name, $drink_flavor_type, $liquid_color, $liquid_color_transparent, $calories_for_100g, $alcohol_vol, $drink_ingredients, $pending_drink_commits_timestamp)
    {
        $select = $this->getSelect();
                if ($pending_drink_commits_userid != null) {
        	$select->where->like('pending_drink_commits_userid' ,'%'.$pending_drink_commits_userid.'%');
        }
                if ($drink_brand != null) {
        	$select->where->like('drink_brand' ,'%'.$drink_brand.'%');
        }
                if ($drink_type != null) {
        	$select->where->like('drink_type' ,'%'.$drink_type.'%');
        }
                if ($company_name != null) {
        	$select->where->like('company_name' ,'%'.$company_name.'%');
        }
                if ($drink_flavor_type != null) {
        	$select->where->like('drink_flavor_type' ,'%'.$drink_flavor_type.'%');
        }
                if ($liquid_color != null) {
        	$select->where->like('liquid_color' ,'%'.$liquid_color.'%');
        }
                if ($liquid_color_transparent != null) {
        	$select->where->like('liquid_color_transparent' ,'%'.$liquid_color_transparent.'%');
        }
                if ($calories_for_100g != null) {
        	$select->where->like('calories_for_100g' ,'%'.$calories_for_100g.'%');
        }
                if ($alcohol_vol != null) {
        	$select->where->like('alcohol_vol' ,'%'.$alcohol_vol.'%');
        }
                if ($drink_ingredients != null) {
        	$select->where->like('drink_ingredients' ,'%'.$drink_ingredients.'%');
        }
                if ($pending_drink_commits_timestamp != null) {
        	$select->where->like('pending_drink_commits_timestamp' ,'%'.$pending_drink_commits_timestamp.'%');
        }
                $statement = $this->getSql()->prepareStatementForSqlObject($select);
        $result = $statement->execute();
        $ret = $result->current();
        if ($ret !== false) {
        	$ret = array($ret);
            while (($line=$result->next()) !== false ) {
        		$ret[]=$line;
        	}
        }
        return $ret;
    }
    

    public function savePendingDrinkCommits(PendingDrinkCommits $pending_drink_commits)
    {
        $data = array(
        	            'pending_drink_commits_userid' => $pending_drink_commits->pending_drink_commits_userid,
                        'drink_brand' => $pending_drink_commits->drink_brand,
                        'drink_type' => $pending_drink_commits->drink_type,
                        'company_name' => $pending_drink_commits->company_name,
                        'drink_flavor_type' => $pending_drink_commits->drink_flavor_type,
                        'liquid_color' => $pending_drink_commits->liquid_color,
                        'liquid_color_transparent' => $pending_drink_commits->liquid_color_transparent,
                        'calories_for_100g' => $pending_drink_commits->calories_for_100g,
                        'alcohol_vol' => $pending_drink_commits->alcohol_vol,
                        'drink_ingredients' => $pending_drink_commits->drink_ingredients,
                        'pending_drink_commits_timestamp' => $pending_drink_commits->pending_drink_commits_timestamp,
                    );

        $id = (int)$pending_drink_commits->id;
        if ($id == 0) {
            $this->insert($data);
        } else {
            if ($this->getPendingDrinkCommits($id)) {
                $this->update($data, array('pending_drink_commits_id' => $id));
            } else {
                throw new \Exception('Form id does not exit');
            }
        }
    }

    public function addPendingDrinkCommits($pending_drink_commits_userid = null, $drink_brand = null, $drink_type = null, $company_name = null, $drink_flavor_type = null, $liquid_color = null, $liquid_color_transparent = null, $calories_for_100g = null, $alcohol_vol = null, $drink_ingredients = null, $pending_drink_commits_timestamp = null)
    {
        $data = array(        );
                if ($pending_drink_commits_userid != null) {
        	$data['pending_drink_commits_userid'] = $pending_drink_commits_userid;
        }
                if ($drink_brand != null) {
        	$data['drink_brand'] = $drink_brand;
        }
                if ($drink_type != null) {
        	$data['drink_type'] = $drink_type;
        }
                if ($company_name != null) {
        	$data['company_name'] = $company_name;
        }
                if ($drink_flavor_type != null) {
        	$data['drink_flavor_type'] = $drink_flavor_type;
        }
                if ($liquid_color != null) {
        	$data['liquid_color'] = $liquid_color;
        }
                if ($liquid_color_transparent != null) {
        	$data['liquid_color_transparent'] = $liquid_color_transparent;
        }
                if ($calories_for_100g != null) {
        	$data['calories_for_100g'] = $calories_for_100g;
        }
                if ($alcohol_vol != null) {
        	$data['alcohol_vol'] = $alcohol_vol;
        }
                if ($drink_ingredients != null) {
        	$data['drink_ingredients'] = $drink_ingredients;
        }
                if ($pending_drink_commits_timestamp != null) {
        	$data['pending_drink_commits_timestamp'] = $pending_drink_commits_timestamp;
        }
                $affectedRows=$this->insert($data);
                return $affectedRows;
            }
    
   
    public function updatePendingDrinkCommits($pending_drink_commits_id, $pending_drink_commits_userid, $drink_brand, $drink_type, $company_name, $drink_flavor_type, $liquid_color, $liquid_color_transparent, $calories_for_100g, $alcohol_vol, $drink_ingredients, $pending_drink_commits_timestamp)
    {
        $data = array(
        	            'pending_drink_commits_userid' => $pending_drink_commits->pending_drink_commits_userid,
                        'drink_brand' => $pending_drink_commits->drink_brand,
                        'drink_type' => $pending_drink_commits->drink_type,
                        'company_name' => $pending_drink_commits->company_name,
                        'drink_flavor_type' => $pending_drink_commits->drink_flavor_type,
                        'liquid_color' => $pending_drink_commits->liquid_color,
                        'liquid_color_transparent' => $pending_drink_commits->liquid_color_transparent,
                        'calories_for_100g' => $pending_drink_commits->calories_for_100g,
                        'alcohol_vol' => $pending_drink_commits->alcohol_vol,
                        'drink_ingredients' => $pending_drink_commits->drink_ingredients,
                        'pending_drink_commits_timestamp' => $pending_drink_commits->pending_drink_commits_timestamp,
                            );
        $this->update($data, array(pending_drink_commits_id => $id));
    }

    public function deletePendingDrinkCommits($id)
    {
        $this->delete(array('pending_drink_commits_id' => $id));
    }

}
